import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, ActionSheetController, Platform, ToastController } from 'ionic-angular';
import { BaseUI } from '../../common/baseui';

//导入四个外部组件
import { File } from '@ionic-native/file';
import { FilePath } from '@ionic-native/file-path';
import { FileTransfer, FileTransferObject } from '@ionic-native/file-transfer';
import { Camera } from '@ionic-native/camera';

declare var cordova;
/**
 * Generated class for the HeadfacePage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

// @IonicPage()
@Component({
    selector: 'page-headface',
    templateUrl: 'headface.html'
})
export class HeadfacePage extends BaseUI {
    userId: string;
    errorMessage: string;
    lastImage: string;
    str:any=null;

    constructor(
        public navCtrl: NavController,
        public navParams: NavParams,
        public actionSheetCtrl: ActionSheetController,
        public file: File,
        public filePath: FilePath,
        public fileTransfer: FileTransfer,
        public camera: Camera,
        public platform: Platform,
        public toastCtrl: ToastController
    ) {
        super();
    }

    ionViewDidEnter() {
        console.log('ionViewDidLoad HeadfacePage');
    }
    presentActionSheet() {
        let actionSheet = this.actionSheetCtrl.create({
            title: '选择图片',
            buttons: [
                {
                    text: '从图片库中选择',
                    handler: () => {
                      this.takePicture(this.camera.PictureSourceType.PHOTOLIBRARY)
                    }
                },
                {
                    text: '使用相机',
                    handler: () => {
                      this.takePicture(this.camera.PictureSourceType.CAMERA)
                    }
                },
                {
                    text: '取消',
                    role: 'cancel'
                }
            ]
        });
        actionSheet.present();
    }
    takePicture(sourceType) {
        //定义相机的参数
        let options = {
            quality: 100,
            sourceType: sourceType,
            destinationType:0,
            saveToPhotoAlbum: false, //保存相册
            correctOrientaiton: true //开启陀螺仪
        };
        this.camera
            .getPicture(options)
            .then((imagePath) => {

                this.str='data:image/png;base64,'+imagePath

            })
            .catch((err) => {
                super.showToast(this.toastCtrl, '选择图片出现错误,请在app中操作,或者赋予权限');
            });
    }
    copyFileLocalDir(namePath,currentName,newFileName) {

      this.file.copyFile(namePath,currentName,cordova.file.dataDirectory,newFileName).then(success=>{
        this.lastImage=newFileName
      }).catch(err=>{
        super.showToast(this.toastCtrl, '存储图片到本地图片出错');
      })
    }
    creatFileName(){
      var d=new Date(),
      n=d.getTime(),
      newFileName=n+'.jpg';
      return newFileName;

    }
    pathForImage(img){
      if(img===null){
        return ''
      }else{
        return cordova.file.dataDirectory+img;
      }
    }

    uploadImage(){
      var url='http://cc.ruiqinet.com/api/index/upload';
      var targetPath=this.pathForImage(this.lastImage);
      var filename='1.jpg';
      var options={
        fileKey:'file',
        fileName:filename,
        chunkedMode:false,
        mimeType:'multipart/form-data',
        params:{
          'fileName':filename
        }
      }
      
    }

    ionViewWillEnter(){
        let elements = document.querySelectorAll(".tabbar");
        if (elements != null) {
            Object.keys(elements).map((key) => {
                elements[key].style.display = 'none';
            });
        }   
      }
    
    ionViewWillLeave() {
        let elements = document.querySelectorAll(".tabbar");
        if (elements != null) {
            Object.keys(elements).map((key) => {
                elements[key].style.display = 'flex';
            });
        }
    } 
}
